package com.seio.edc.controller.system;

import com.alibaba.fastjson.JSON;
import com.nova.edc.model.system.*;
import com.nova.edc.service.system.AreaPointService;
import com.nova.edc.service.system.CustomerAreaService;
import com.nova.edc.service.system.DeviceLightService;
import com.nova.edc.service.system.PointLightService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/systemPointLight")
public class PointLightContronller {
    @Autowired
    private DeviceLightService novaDeviceLightService;

    @Autowired
    private PointLightService pointLightService;

    @Autowired
    private AreaPointService novaAreaPointService;

    @Autowired
    private CustomerAreaService novaCustomerAreaService;


    private static final int PAGE_SIZE = 10;

    // 初始化灯具管理页面
    @RequestMapping(value = "PointLightList.shtml")
    public ModelAndView pointLightList(HttpServletRequest request,
                                       HttpSession session, Integer pageIndex, PointLight pointLight,
                                       ModelMap modelMap) {
        // 当前页数
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        PageList<PointLight> pageList = pointLightService
                .findPointLightPageList(pointLight, page, "id", "asc");
        AreaPoint areaPoint = novaAreaPointService.findById(pointLight.getPointid());
        CustomerArea customerArea = novaCustomerAreaService.findById(areaPoint.getAreaid());

        modelMap.put("customerArea", customerArea);
        modelMap.put("areaPoint", areaPoint);
        modelMap.put("pointLight", pointLight);
        modelMap.put("pointLights", pageList.getDatas());
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        return new ModelAndView("system/pointLightList", modelMap);
    }

    // 编辑灯具页面
    @RequestMapping(value = "PointLightEdit.shtml")
    public ModelAndView pointLightEdit(HttpServletRequest request,
                                       HttpSession session, Integer pointid, Integer id,
                                       ModelMap modelMap) {
        if (id != null && id != 0) {
            PointLight pointLight1 = pointLightService.findById(id);
            modelMap.put("pointLight", pointLight1);
        }
        AreaPoint areaPoint = novaAreaPointService.findById(pointid);
        CustomerArea customerArea = novaCustomerAreaService.findById(areaPoint.getAreaid());
        List<DeviceLight> traLightList = novaDeviceLightService
                .findLightEnergy(ModelConstant.TRA_LIGHTTYPE);
        List<DeviceLight> ledLightList = novaDeviceLightService
                .findLightEnergy(ModelConstant.LED_LIGHTTYPE);
        if (traLightList != null && traLightList.size() > 0) {
            modelMap.put("traLightCodeInfo", JSON
                    .toJSONString(novaDeviceLightService
                            .getLightCodeByList(traLightList)));
        }
        if (ledLightList != null && ledLightList.size() > 0) {
            modelMap.put("ledLightCodeInfo", JSON
                    .toJSONString(novaDeviceLightService
                            .getLightCodeByList(ledLightList)));
        }
        modelMap.put("tralight", ModelConstant.TRA_LIGHTTYPE);
        modelMap.put("ledlight", ModelConstant.LED_LIGHTTYPE);
        modelMap.put("customerArea", customerArea);
        modelMap.put("areaPoint", areaPoint);
        return new ModelAndView("system/pointLightEdit", modelMap);
    }

    // 确认更改
    @RequestMapping(value = "savePointLight.shtml")
    public ModelAndView savePointLight(HttpServletRequest request,
                                       HttpSession session, PointLight pointLight,
                                       ModelMap modelMap) {
        if (pointLight.getId() != null && pointLight.getId() != 0) {
            pointLightService.update(pointLight);
        } else {
            pointLightService.insert(pointLight);
        }
        PointLight pointLight1 = new PointLight();
        pointLight1.setPointid(pointLight.getPointid());
        return pointLightList(request, session, 1, pointLight1, modelMap);

    }

    //删除
    @RequestMapping(value = "delPointLight.shtml")
    public ModelAndView delPointLight(HttpServletRequest request,
                                      HttpSession session, Integer pointid, Integer id,
                                      ModelMap modelMap) {
        pointLightService.deleteById(id);
        PointLight pointLight2 = new PointLight();
        pointLight2.setPointid(pointid);
        return pointLightList(request, session, 1, pointLight2, modelMap);
    }
}
